package com.zengge.zbc.utils.http;

import okhttp3.Response;
import okhttp3.ResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

@Service
public class CallbackProxy {

    private final static Logger logger = LoggerFactory.getLogger(CallbackProxy.class);

    @Transactional
    public void onSucceed(Callback callback, Response response) {
        try {
            if(callback!=null){
                int code = response.code();
                ResponseBody responseBody = response.body();
                callback.onSucceed(code,responseBody);
            }
        } catch (IOException e) {
           logger.error(e.getMessage(),e);
        } finally {
            response.close();
        }

    }

    @Transactional
    public void onFailure(Callback callback, IOException e) {
        if(callback!=null){
            callback.onFailure(e);
        }
    }
}
